package com.zisu.aradvertising.service.impl;

import com.zisu.aradvertising.pojo.AdvertiserSpace;
import com.zisu.aradvertising.pojo.Cooperation;
import com.zisu.aradvertising.pojo.Sign;
import com.zisu.aradvertising.repository.*;
import com.zisu.aradvertising.service.CooperationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CooperationServiceImpl implements CooperationService {
    //实例化抽象类
    @Resource
    private DaoSign daoSign;
    @Resource
    private DaoCooperation daoCooperation;
    @Resource
    private DaoAdvertiserSpace daoAdvertiserSpace;
    @Resource
    private DaoProduct daoProduct;
    @Resource
    private DaoVideo daoVideo;


    @Override
    //实现新增加或编辑合作逻辑
    public Cooperation editCooperationService(Cooperation cooperation){
        System.out.println(0);
        Cooperation newcooperation =daoCooperation.findByCoid(Math.toIntExact(cooperation.getCoid()));
        System.out.println(-1);
        cooperation.setPid(daoProduct.findByPname(cooperation.getProductname()).getPid());
        if (newcooperation!=null ){
            System.out.println(1);
            cooperation.setCostate(newcooperation.getCostate());
            System.out.println(2);
            daoCooperation.save(cooperation);
            System.out.println(3);
            return cooperation;
        }else{

            cooperation.setCostate("待确认");
            Cooperation c1=daoCooperation.save(cooperation);
            Sign sign=new Sign();
            sign.setAid(daoProduct.findByPid(cooperation.getPid()).getAid());
            sign.setCid(daoVideo.findByVid(daoAdvertiserSpace.findByApid(cooperation.getApid()).getVid()).getCid());
            sign.setCoid(c1.getCoid());
            daoSign.save(sign);
            return c1;
        }
    }

    @Override
    //实现取消合作逻辑
    public Cooperation cancelCooperationService(int Coid){
        Cooperation cooperation=daoCooperation.findByCoid(Coid);
        cooperation.setCostate("已终止");
        daoCooperation.save(cooperation);
        return cooperation;
    }

    @Override
    //广告商历史订单
    public  List<Cooperation> findAllCoByAid(int Aid){
        List<Cooperation> listcooperation= new ArrayList<>();
        List<Sign> sign=(daoSign.findAllByAid(Math.toIntExact(Aid)));
        if(sign==null){return null;}
        List<Integer> listcoid= new ArrayList<>();
        for(Sign x:sign){
            listcoid.add(x.getCoid());
        }
        for (Integer x:listcoid){
            listcooperation.add(daoCooperation.findByCoid(x));
        }
        if(listcooperation.size()!=0){
        return listcooperation;}
        else {
            return null;
        }
    }

    //up主历史订单
    @Override
    public  List<Cooperation> findAllCoByCid(int Cid){
        List<Cooperation> listcooperation= new ArrayList<>();
        List<Sign> sign=(daoSign.findAllByCid(Math.toIntExact(Cid)));
        if(sign==null){return null;}
        List<Integer> listcoid= new ArrayList<>();
        for(Sign x:sign){
            listcoid.add(x.getCoid());
        }
        for (Integer x:listcoid){
            listcooperation.add(daoCooperation.findByCoid(x));
        }
        if(listcooperation.size()!=0){
            return listcooperation;}
        else {
            return null;
        }
    }

    @Override
    //同意订单合作逻辑
    public Cooperation agree(int Coid){
        Cooperation cooperation=daoCooperation.findByCoid(Coid);
        cooperation.setCostate("进行中");
        AdvertiserSpace advertiserSpace=daoAdvertiserSpace.findByApid(cooperation.getApid());
        advertiserSpace.setApsumcoreaption(advertiserSpace.getApsumcoreaption()+1);
        daoAdvertiserSpace.save(advertiserSpace);
        return cooperation;
    }

    @Override
    //拒绝合作逻辑
    public Cooperation refuse(int Coid){
        Cooperation cooperation=daoCooperation.findByCoid(Coid);
        cooperation.setCostate("已拒绝");
        AdvertiserSpace advertiserSpace=daoAdvertiserSpace.findByApid(cooperation.getApid());
        advertiserSpace.setApsumcoreaption(advertiserSpace.getApsumcoreaption()+1);
        daoAdvertiserSpace.save(advertiserSpace);
        return cooperation;
    }

    @Override
    //删除合作逻辑
    public void delete(int coid){
        daoCooperation.deleteByCoid(coid);
    }

}
